package com.gemptc.service.iphone;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import com.gemptc.daoimp.InsertImp;
import com.gemptc.daoimp.SelectImp;
import com.gemptc.daoimp.UpdateImp;


/**
 * Servlet implementation class Register
 */
@WebServlet("/Register")
public class Register extends HttpServlet {
	private static final long serialVersionUID = 1L;
	JSONObject jsonObject;
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Register() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		SelectImp selectDaoImp = new SelectImp();
		PrintWriter printWriter=response.getWriter();
		String user_name = request.getParameter("user_name");
		String user_nickname = request.getParameter("user_nickname");
		String user_password = request.getParameter("user_password");
		Date currentTime = new Date();
		String date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
				.format(currentTime);
		boolean falg = selectDaoImp.select2(user_name);
		if (falg) {
			UpdateImp updateImp=new UpdateImp();
			boolean chenggong=updateImp.update(user_password, user_name);
			if (chenggong) {
				printWriter.write("chenggong");
			}else {
				printWriter.write("sorry");
			}
		} else {
			
			InsertImp insertUserdaoimp = new InsertImp();
			boolean isSuccess = insertUserdaoimp.insert(user_name, user_nickname,
					user_password, date);
			
			if (isSuccess) {
				printWriter.write("success");	
				
			} else {
				printWriter.write("error");	
				
			}
		}
	}
	
}
